cmake_minimum_required(VERSION 3.15)
project(LogCollectorService)

set(CMAKE_CXX_STANDARD 17)

set(SOURCES
    main.cpp
    src/DbusManager.cpp
    src/DbusContext.cpp
    src/LoggerService.cpp
    src/LocalLoggerHandler.cpp
    src/JournalLoggerHandler.cpp
)

set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

# 查找 systemd 库
# 需要安装 PkgConfig库 sudo apt-get install pkg-config
find_package(PkgConfig REQUIRED)
pkg_check_modules(SYSTEMD REQUIRED libsystemd)

add_executable(LogCollectorService
    ${SOURCES}
)

target_include_directories(LogCollectorService PUBLIC ${INCLUDE_DIR})

# 加入systemd的include目录
target_include_directories(LogCollectorService PRIVATE ${SYSTEMD_INCLUDE_DIRS})

# 加入lib库include目录
target_include_directories(LogCollectorService PRIVATE LogCollector)

# 加入编译option
target_compile_options(LogCollectorService PRIVATE ${SYSTEMD_CFLAGS_OTHER})

# 链接systemd库
target_link_libraries(LogCollectorService PRIVATE ${SYSTEMD_LIBRARIES})

# 链接lib库
target_link_libraries(LogCollectorService PRIVATE LogCollector)

set(BINDIR /opt/shz/bin)
set(SYSTEMD_UNITDIR /lib/systemd/system)
set(DBUS_CONF_DIR /etc/dbus-1/system.d)

# 将可执行文件安装到 /opt/shz/bin
install(TARGETS LogCollectorService
    RUNTIME DESTINATION ${BINDIR}
)

# 将service安装到 /lib/systemd/system
install(FILES LogCollector.service
    DESTINATION ${SYSTEMD_UNITDIR}
)

install(FILES com.shz.LogCollectorService.conf
    DESTINATION ${DBUS_CONF_DIR}
)

# sudo make install & sudo make post_install 可以执行以下命令
add_custom_target(post_install
    COMMAND sudo ldconfig                       # 重新搜索并链接 .so
    COMMAND sudo systemctl reload dbus
    COMMAND sudo systemctl daemon-reload
    COMMAND sudo systemctl enable LogCollector
    COMMAND sudo systemctl stop LogCollector
    COMMAND sudo systemctl start LogCollector
    COMMENT "Reloading systemd units"
)

# public
# target_link_libraries(LogCollectorService PUBLIC library)